import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: 'router',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(),
      //注册路由 上面的home必须新建一个class，不能直接写在这里
      routes: <String, WidgetBuilder> {
        'new_page': (context)=>NewRouter(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyHomePageState();
  }
}


class MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _addCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        appBar: new AppBar(
          title: Text('router管理'),
          backgroundColor: Colors.orange,
        ),
        body: new Container(
          margin: EdgeInsets.all(30),
          decoration: BoxDecoration(color: Colors.greenAccent),
          width: double.infinity,
          height: double.infinity,
          child: new Stack(
            alignment: Alignment.topLeft,
            children: <Widget>[
              new Positioned(
                right: 10,
                bottom: 10,
                child: new Container(
                  decoration: BoxDecoration(color: Colors.red),
                  constraints: BoxConstraints(
                    maxHeight: 100,
                    maxWidth: 100,
                    minHeight: 100,
                    minWidth: 100
                  ),
                  child: FloatingActionButton(
                    child: Icon(Icons.add),
                    onPressed: () {
                      _addCounter();
                    },

                  ),
                ),
              ),
              new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(
                    'You have pushed the button this many times:',
                    style: TextStyle(fontSize: 20),
                    textAlign: TextAlign.center,
                  ),
                  new Text(
                    '$_counter',
                    style: TextStyle(fontSize: 40),
                    textAlign: TextAlign.center,
                  ),
                  new FlatButton(
                    child: new Text(
                      'open new route',
                      style: TextStyle(fontSize: 20, color: Colors.blue),
                    ),
                    onPressed: () {
//                      Navigator.push(context,
//                          new MaterialPageRoute(builder: (context) {
//                            return new NewRouter();
//                          }));
//                      Navigator.pushNamed(context, 'new_page');
                      Navigator.of(context).pushNamed('new_page', arguments: 'hi');
                    },
                  )
                ],
              )
            ],
          ),
        )
    );
  }
}

class NewRouter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: new AppBar(
        title: new Text('New router'),
        backgroundColor: Colors.red,
      ),
      body: new EchoRoute()
    );
  }
}

class EchoRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    //获取路由参数
    var args = ModalRoute.of(context).settings.arguments;
    print(args);
    return new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text('This is new router', style: Theme
                .of(context)
                .textTheme
                .display1,),
            new FlatButton(
              child: new Text(
                  'pop', style: TextStyle(fontSize: 30, color: Colors.blue)),
              onPressed: () {
                Navigator.pop(context);
              },
            )
          ],
        )
    );
  }
}

